<template>
  <div class="charts" ref="chartRef"></div>
</template>

<script>
import echarts from 'echarts';
let myCharts = null;
export default {
  name: 'HorizontalBar',
  props: {
    list: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    list: {
      handler(newVal) {
        if (!newVal || newVal.length === 0) return;
        this.$nextTick(() => {
          myCharts.setOption(this.getOptions(newVal));
        });
      },
      immediate: true,
      deep: true,
    },
  },
  mounted() {
    myCharts = echarts.init(this.$refs.chartRef);
  },
  methods: {
    getOptions(data) {
      const yData = data.map(item => item.storeName);
      const seriesData = data.map(item => item.orderAmountNum);
      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        grid: {
          top: -10,
          left: '4%',
          right: '4%',
          bottom: '2%',
          containLabel: true,
        },
        xAxis: [
          {
            type: 'value',
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                color: 'rgba(101, 130, 146, 0.3)',
                type: 'dashed',
              },
            },
          },
          {
            show: false,
          },
        ],
        yAxis: [
          {
            type: 'category',
            data: yData,
            inverse: true,
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            axisLine: {
              show: false,
            },
          },
        ],
        color: [
          new echarts.graphic.LinearGradient(1, 0, 0, 0, [
            { offset: 0, color: '#1982E5' },
            { offset: 1, color: '#0038CF' },
          ]),
        ],
        series: [
          {
            name: '',
            type: 'bar',
            barGap: '-100%',
            xAxisIndex: 0,
            barWidth: 10,
            data: seriesData,
          },
          {
            type: 'bar',
            xAxisIndex: 1,
            barGap: '-100%',
            barWidth: 10,
            label: {
              show: true,
              position: [0, '-15'],
              fontSize: 12,
              color: '#fff',
              formatter: (params) => {
                const cur = data.find(item => item.storeName === params.name);  
                if (cur && cur.orderAmountNum) {
                  return params.name + `（${cur.orderAmountNum}）`;
                } else {
                  return params.name;
                }
              },
            },
            itemStyle: {
              color: 'transparent',
            },
            data: [200, 200, 200, 200, 200],
            tooltip: {
              show: false,
            },
          },
        ],
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.charts {
  width: 100%;
  height: 100%;
}
</style>
